package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.ImagePattern;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
 * @ClassName ImagePatternDemo
 * @Description
 * @Author wangss
 * @date 2020.07.01 12:38
 * @Version 1.0
 */
public class ImagePatternDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Image image = new Image("/images/24.png");

        AnchorPane an = new AnchorPane();

        GridPane gridPane = new GridPane();
        gridPane.setHgap(10d);
        gridPane.setVgap(10d);

        for (int i = 0; i < 5; i++) {
            Rectangle rec = new Rectangle(150, 150);
            rec.setFill(Paint.valueOf("#EDEDED"));
            gridPane.add(rec, i, 0);
        }

        HBox hBox = new HBox(20);
        hBox.getChildren().add(new Circle(150));
        hBox.getChildren().add(new Polygon(150, 0, 0, 300, 300, 300));

        an.getChildren().addAll(gridPane, hBox);
        AnchorPane.setTopAnchor(hBox, 400d);

        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setHeight(1000d);
        primaryStage.setWidth(1000d);
        primaryStage.setAlwaysOnTop(true);
        primaryStage.show();

        Rectangle r1 = (Rectangle) gridPane.getChildren().get(0);
        r1.setFill(new ImagePattern(image));

        Rectangle r2 = (Rectangle) gridPane.getChildren().get(1);
        r2.setFill(new ImagePattern(image, 0, 0, 0.5, 0.5, true));

        Rectangle r3 = (Rectangle) gridPane.getChildren().get(2);
        r3.setFill(new ImagePattern(image, 0, 0, 100, 100, false));

        Rectangle r4 = (Rectangle) gridPane.getChildren().get(3);
        r4.setFill(new ImagePattern(image, 50, 50, 100, 100, false));

        Rectangle r5 = (Rectangle) gridPane.getChildren().get(4);
        r5.setFill(new ImagePattern(image, 150, 50, 100, 100, false));

        Circle circle = (Circle) hBox.getChildren().get(0);
        circle.setFill(new ImagePattern(image, 0, 0, 100, 100, false));

        Polygon polygon = (Polygon) hBox.getChildren().get(1);
        polygon.setFill(new ImagePattern(image, 0, 0, 0.5, 0.5, true));
    }
}
